// preload.js
const { contextBridge, ipcRenderer } = require('electron');
const remote = require('@electron/remote');

const api = {
    sendMessage: (channel, args) => ipcRenderer.send(channel, args),
    on: (channel, listener) => {
        // 添加监听器
        const subscription = { channel, listener };
        ipcRenderer.on(channel, (event, ...args) => listener(...args));
        // 返回一个取消订阅的函数
        return () => {
            ipcRenderer.removeListener(channel, subscription.listener);
        };
    },
};
const electronAPI = {
    getCurrentWindow: () => {
        const currentWindow = remote.getCurrentWindow();
        return {
            minimize: () => currentWindow.minimize(),
            maximize: () => currentWindow.maximize(),
            close: () => currentWindow.close(),
        };
    },
}

contextBridge.exposeInMainWorld('electron', api);

contextBridge.exposeInMainWorld('electronAPI', electronAPI);
